import {setSystemSetting,getSystemSetting,getPrintSetting,setPrintSetting,setCheckTreatment,getCheckTreatment,setOtherTreatment,getOtherTreatment,getBillCheck,setBillCheck} from "../../utils/localStorageData";
import {getSysDic} from "../../api/common";
import {checkTreatment} from "../../api/clinicManagement";

const state = {
  systemSetting:getSystemSetting(),
  printSetting:getPrintSetting(),
  checkTreatment:getCheckTreatment(),
  otherTreatment:getOtherTreatment(),
  billCheck:getBillCheck()
}

const mutations = {
  SET_SYSTEMSETTING: (state, data) => {
    state.systemSetting = data
  },
  SET_PRINTSETTING: (state, data) => {
    state.printSetting = data
  },
  SET_CHECKTREATMENT: (state, data) => {
    state.checkTreatment = data
  },
  SET_OTHERTREATMENT: (state, data) => {
    state.otherTreatment = data
  },
  SET_BILLCHECK: (state, data) => {
    state.billCheck = data
  },
}

const actions = {
  setBillCheck({ commit },data){
    commit('SET_BILLCHECK', data)
    setBillCheck(data);
  },
  setAllSetting({ commit }){
    let p1 = new Promise((resolve, reject) => {
      getSysDic({key:"function_setting",type:1})
        .then(res => {
          if (res.code == 200){
            commit('SET_SYSTEMSETTING', res.data)
            setSystemSetting(res.data)
            resolve()
          }else {
            reject(res)
          }
          // const { token } = res.entry
          // commit('SET_TOKEN', token)
          // setToken(token)

        })
        .catch(error => {
          reject(error)
        }).catch((e) => {});
    })
    let p2 = new Promise((resolve, reject) => {
      getSysDic({key:"print_setting",type:2})
        .then(res => {
          if (res.code == 200){
            commit('SET_PRINTSETTING', res.data)
            setPrintSetting(res.data)
            resolve()
          }else {
            reject(res)
          }
          // const { token } = res.entry
          // commit('SET_TOKEN', token)
          // setToken(token)

        })
        .catch(error => {
          reject(error)
        })
    }).catch((e) => {});
    return Promise.all([p1,p2])
  },
  // 保存系统设置
  setSystemSetting({ commit }) {
    console.log(1111)
    return new Promise((resolve, reject) => {
      getSysDic({key:"function_setting",type:1})
        .then(res => {
          if (res.code == 200){
            commit('SET_SYSTEMSETTING', res.data)
            setSystemSetting(res.data)
            resolve()
          }else {
            reject(res)
          }
          // const { token } = res.entry
          // commit('SET_TOKEN', token)
          // setToken(token)

        })
        .catch(error => {
          reject(error)
        })
    })
  },
  // 保存打印设置
  setPrintSetting({ commit }) {

    return new Promise((resolve, reject) => {
      getSysDic({key:"print_setting",type:2})
        .then(res => {
          if (res.code == 200){
            commit('SET_PRINTSETTING', res.data)
            setPrintSetting(res.data)
            resolve()
          }else {
            reject(res)
          }
          // const { token } = res.entry
          // commit('SET_TOKEN', token)
          // setToken(token)

        })
        .catch(error => {
          reject(error)
        })
    })
  },
  //检查治疗项-附加费用项：获取
  updataCheckTreatment({ commit },type){
    return new Promise((resolve, reject) => {
      checkTreatment({type:type})
        .then(res => {
          if (res.code == 200){
            if (type == 1){
              commit("SET_CHECKTREATMENT",res.data);
              setCheckTreatment(res.data);
            }else if (type == 2){
              commit("SET_OTHERTREATMENT",res.data);
              setOtherTreatment(res.data);
            }
            resolve()
          }else {
            reject(res)
          }

        })
        .catch(error => {
          reject(error)
        })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
